home *** CD-ROM | disk | FTP | other *** search
/ Amiga Inside! / Amiga FD Inside (1995)(Ultramax).iso / berndspd / devtools / intuigen / intuigenlibs / editlist.c next >
Encoding:
C/C++ Source or Header  |  1993-12-22  |  13.1 KB  |  624 lines

  1. /*
  2. EditList.c
  3.  
  4. (C) Copyright 1993 Justin Miller
  5.     This file is part of the IntuiGen package.
  6.     Use of this code is pursuant to the license outlined in
  7.     COPYRIGHT.txt, included with the IntuiGen package.
  8.  
  9.     As per COPYRIGHT.txt:
  10.  
  11.     1)  This file may be freely distributed providing that
  12.         it is unmodified, and included in a complete IntuiGen
  13.         2.0 package (it may not be distributed alone).
  14.  
  15.     2)  Programs using this code may not be distributed unless
  16.         their author has paid the Shareware fee for IntuiGen 2.0.
  17. */
  18.  
  19. #include <exec/exec.h>
  20. #include <clib/exec_protos.h>
  21. #include <libraries/GadTools.h>
  22. #include <clib/intuition_protos.h>
  23. #include <intuition/gadgetclass.h>
  24. #include <IntuiGen/GTRequest.h>
  25.  
  26. #define FirstNode(l) FirstItem(l)
  27. #define NextNode(n) NextItem(n)
  28.  
  29. #define ALPHA 1
  30. #define UPDATING 2
  31. #define CALLBACK 4
  32.  
  33. struct EditListInfo {
  34.     struct GTControl *Main,*Listc,*Add,*Delete,*Edit;
  35.     struct List *List;
  36.     struct Remember **Key;
  37.     void (*SetLabels) (struct GTRequest *,struct GTControl *,struct List *);
  38.     ULONG Size;
  39.     ULONG Flags;
  40. };
  41.  
  42. struct Node *FirstItem(struct List *l);
  43. struct Node *NextItem(struct Node *n);
  44.  
  45.  
  46.  
  47. static void DeleteGadgetUp(struct GTRequest *req,struct IntuiMessage *msg,
  48.             struct GTControl *gtc,struct MessageHandler *mh);
  49.  
  50. static void NewGadgetUp(struct GTRequest *req,struct IntuiMessage *msg,
  51.             struct GTControl *gtc,struct MessageHandler *mh);
  52.  
  53. static void EditorDSelected(struct GTRequest *req,struct IntuiMessage *msg,
  54.             struct GTControl *gtc,struct MessageHandler *mh);
  55. static void ListGadgetUp(struct GTRequest *req,struct IntuiMessage *msg,
  56.             struct GTControl *gtc,struct MessageHandler *mh);
  57.  
  58.  
  59.  
  60. static struct TextAttr TextAttributes0 =
  61. {
  62.     "topaz.font",
  63.     TOPAZ_EIGHTY,
  64.     NULL,
  65.     FPF_ROMFONT
  66. };
  67.  
  68. static struct MessageHandler ListDeleteGadgetUpMH =
  69. {
  70.     NULL,
  71.     "GadgetUp",
  72.     NULL,
  73.     DeleteGadgetUp
  74. };
  75.  
  76. static struct TagItem ListDeleteTags[]=
  77. {
  78.     {  GA_Disabled, 0 },
  79.     {  TAG_DONE,0  }
  80. };
  81.  
  82. static struct NewGadget NewListDelete=
  83. {
  84.     292,119,
  85.     28,13,
  86.     (UBYTE *)"-",
  87.     &TextAttributes0,
  88.     1,
  89.     0,
  90.     0,
  91.     0
  92. };
  93.  
  94. static struct GTControl ListDelete =
  95. {
  96.     NULL,
  97.     BUTTON_KIND,
  98.     GTC_INTERNALCONTROL,
  99.     ListDeleteTags,
  100.     NULL,
  101.     &NewListDelete,
  102.     NULL,
  103.     &ListDeleteGadgetUpMH,
  104.     '',
  105.     0,
  106.     0,
  107.     0,0,
  108.     NULL,
  109.     NULL,
  110.     NULL,
  111.     0,0,
  112.     0,
  113.     0,
  114.     NULL,
  115.     NULL,
  116.     NULL,
  117.     NULL,
  118.     NULL,
  119.     NULL,
  120.     NULL
  121. };
  122.  
  123. static struct MessageHandler ListAddGadgetUpMH =
  124. {
  125.     NULL,
  126.     "GadgetUp",
  127.     NULL,
  128.     NewGadgetUp
  129. };
  130.  
  131. static struct TagItem ListAddTags[]=
  132. {
  133.     {  GA_Disabled, 0 },
  134.     {  TAG_DONE,0  }
  135. };
  136.  
  137. static struct NewGadget NewListAdd=
  138. {
  139.     292,105,
  140.     28,13,
  141.     (UBYTE *)"+",
  142.     &TextAttributes0,
  143.     0,
  144.     0,
  145.     0,
  146.     0
  147. };
  148.  
  149. static struct GTControl ListAdd =
  150. {
  151.     &ListDelete,
  152.     BUTTON_KIND,
  153.     GTC_INTERNALCONTROL,
  154.     ListAddTags,
  155.     NULL,
  156.     &NewListAdd,
  157.     NULL,
  158.     &ListAddGadgetUpMH,
  159.     '',
  160.     0,
  161.     0,
  162.     0,0,
  163.     NULL,
  164.     NULL,
  165.     NULL,
  166.     0,0,
  167.     0,
  168.     0,
  169.     NULL,
  170.     NULL,
  171.     NULL,
  172.     NULL,
  173.     NULL,
  174.     NULL,
  175.     NULL
  176. };
  177.  
  178. static struct MessageHandler ListEditorDSelectedHandler = {
  179.     NULL,
  180.     "StringDSelected",
  181.     NULL,
  182.     EditorDSelected
  183. };
  184.  
  185. static struct TagItem ListEditorTags[]=
  186. {
  187.     {  GA_Disabled, 0 },
  188.     {  GA_Immediate,1  },
  189.     {  GTST_MaxChars,100  },
  190.     {  STRINGA_Justification,GACT_STRINGLEFT  },
  191.     {  TAG_DONE,0  }
  192. };
  193.  
  194. static struct NewGadget NewListEditor=
  195. {
  196.     100,0,
  197.     150,14,
  198.     (UBYTE *)"",
  199.     &TextAttributes0,
  200.     0,
  201.     0,
  202.     0,
  203.     0
  204. };
  205.  
  206. static struct GTControl ListEditor =
  207. {
  208.     &ListAdd,
  209.     STRING_KIND,
  210.     GTC_INTERNALCONTROL,
  211.     ListEditorTags,
  212.     NULL,
  213.     &NewListEditor,
  214.     NULL,
  215.     &ListEditorDSelectedHandler,
  216.     '',
  217.     0,
  218.     0,
  219.     0,0,
  220.     NULL,
  221.     NULL,
  222.     NULL,
  223.     100,0,
  224.     0,
  225.     0,
  226.     GTST_String,
  227.     NULL,
  228.     NULL,
  229.     NULL,
  230.     NULL,
  231.     NULL,
  232.     NULL
  233. };
  234.  
  235. static struct TagItem ListTags[] =
  236. {
  237.     {  GTLV_Labels, NULL  },
  238.     {  GTLV_Selected, 0  },
  239.     {  GTCT_NEXTDATATOGADGETADDRESS, 0  },
  240.     {  GTLV_ShowSelected, &ListEditor  },
  241.     {  TAG_DONE, 0    }
  242. };
  243.  
  244. static struct NewGadget NewList =
  245. {
  246.     100,0,
  247.     150,100,
  248.     "",
  249.     &TextAttributes0,
  250.     0,
  251.     PLACETEXT_LEFT,
  252.     NULL,
  253.     NULL
  254. };
  255.  
  256. static struct MessageHandler ListGadgetUpMH =
  257. {
  258.     NULL,
  259.     "GadgetUp",
  260.     NULL,
  261.     ListGadgetUp
  262. };
  263.  
  264. static struct GTControl List =
  265. {
  266.     &ListEditor,
  267.     LISTVIEW_KIND,
  268.     GTC_INTERNALCONTROL,
  269.     ListTags,
  270.     NULL,
  271.     &NewList,
  272.     NULL,
  273.     &ListGadgetUpMH,
  274.     '',
  275.     0,
  276.     0,
  277.     0,0,
  278.     NULL,
  279.     NULL,
  280.     NULL,
  281.     0,0,
  282.     0,
  283.     0xffffffff,
  284.     GTLV_Selected,
  285.     NULL,
  286.     NULL,
  287.     NULL,
  288.     NULL,
  289.     NULL,
  290.     NULL
  291. };
  292.  
  293.  
  294. __far void AddInternalControl(struct GTRequest *req,struct GTControl *parent,struct GTControl *gtc,ULONG type);
  295. __far struct GTControl *AllocGTControl(struct GTRequest *req,ULONG AdditionalBytes);
  296.  
  297.  
  298.  
  299. __far static void InternalSetLabels(struct GTRequest *req,
  300.             struct EditListInfo *eli,struct List *list)
  301. {
  302.     if (eli->SetLabels)
  303.     (*(eli->SetLabels)) (req,eli->Main,list);
  304.     else SetControlAttrs(req,eli->Main,GTLV_Labels,list,TAG_DONE);
  305. }
  306.  
  307.  
  308. __far void SetEditListAttrs(struct GTRequest *req,struct GTControl *gtc,
  309.                 struct TagItem *ti)
  310. {
  311.     struct EditListInfo *eli;
  312.     struct TagItem *tstate;
  313.  
  314.     tstate=ti;
  315.  
  316.     eli=(struct EditListInfo *)gtc->ControlTags;
  317.  
  318.     while (ti=NextTagItem(&tstate)) {
  319.     switch(ti->ti_Tag) {
  320.         case GTLV_Labels:
  321.         if ((eli->Flags & UPDATING) && !(eli->Flags & CALLBACK)) {
  322.             eli->Flags |= CALLBACK;
  323.             InternalSetLabels(req,eli,(struct List *)ti->ti_Data);
  324.             eli->Flags ^= CALLBACK;
  325.         } else {
  326.  
  327.             if (!(ti->ti_Data) && !(eli->Flags & UPDATING)) {
  328.             GT_SetGadgetAttrs(eli->Add->Gadget,req->Window,0,
  329.                     GA_Disabled,1,TAG_DONE);
  330.             GT_SetGadgetAttrs(eli->Delete->Gadget,req->Window,0,
  331.                     GA_Disabled,1,TAG_DONE);
  332.             GT_SetGadgetAttrs(eli->Edit->Gadget,req->Window,0,
  333.                     GA_Disabled,1,TAG_DONE);
  334.  
  335.             } else if (!(eli->List) && !(eli->Flags & UPDATING)) {
  336.             GT_SetGadgetAttrs(eli->Add->Gadget,req->Window,0,
  337.                     GA_Disabled,0,TAG_DONE);
  338.             GT_SetGadgetAttrs(eli->Delete->Gadget,req->Window,0,
  339.                     GA_Disabled,0,TAG_DONE);
  340.             GT_SetGadgetAttrs(eli->Edit->Gadget,req->Window,0,
  341.                     GA_Disabled,0,TAG_DONE);
  342.             }
  343.             if (!(eli->Flags & UPDATING)) eli->List=(struct List *)ti->ti_Data;
  344.             GT_SetGadgetAttrs(eli->Listc->Gadget,req->Window,0,
  345.                     GTLV_Labels,ti->ti_Data,TAG_DONE);
  346.         }
  347.         break;
  348.         case GTPK_Remember:
  349.         eli->Key=(struct Remember **)ti->ti_Data;
  350.         break;
  351.         case GTPK_NodeSize:
  352.         eli->Size=ti->ti_Data;
  353.         break;
  354.         case GTPK_Alpha:
  355.         break;
  356.         case GTPK_SetList:
  357.         eli->SetLabels=ti->ti_Data;
  358.         break;
  359.  
  360.         case GTLV_Selected:
  361.         gtc->Attribute=ti->ti_Data;
  362.         default:
  363.         SetControlAttrs(req,eli->Listc, ti->ti_Tag,ti->ti_Data,TAG_DONE);
  364.         break;
  365.     }
  366.     }
  367. }
  368.  
  369. __far struct Gadget *CreateEditListKind(struct GTPKind *kclass,struct Gadget *gad,
  370.                   struct GTControl *gtc,struct GTRequest *req,
  371.                   struct VisualInfo *vinfo)
  372. {
  373.     struct GTControl *list,*add,*rem,*edit;
  374.     struct NewGadget *ng;
  375.     struct TagItem *listtags,*addtags,*remtags,*edittags;
  376.     SHORT x,y,w,h,n;
  377.     struct EditListInfo *eli;
  378.  
  379.     ng=gtc->NewGadget;
  380.     x=ng->ng_LeftEdge;
  381.     y=ng->ng_TopEdge;
  382.     w=ng->ng_Width;
  383.     h=ng->ng_Height;
  384.  
  385.     if (list=AllocGTControl(req,0)) {
  386.     if (add=AllocGTControl(req,0)) {
  387.         if (rem=AllocGTControl(req,0)) {
  388.         if (edit=AllocGTControl(req,0)) {
  389.             *list=List;
  390.             *add=ListAdd;
  391.             *rem=ListDelete;
  392.             *edit=ListEditor;
  393.  
  394.             if (list->NewGadget=AllocNewGadget(req,ng,x,y,w-19,h)) {
  395.             if (add->NewGadget=AllocNewGadget(req,&NewListAdd,
  396.              x+w-18,y+h-18,18,10)) {
  397.                 if (rem->NewGadget=AllocNewGadget(req,&NewListDelete,
  398.                  x+w-18,y+h-7,18,10)) {
  399.                 if (edit->NewGadget=AllocNewGadget(req,&NewListEditor,
  400.                  x,y+h-14,w-19,14)) {
  401.  
  402.                     if (listtags=AllocRemember(&req->GTKey,
  403.                      sizeof(struct TagItem) * 14, MEMF_PUBLIC)) {
  404.                     if (eli=AllocRemember(&req->GTKey,
  405.                      sizeof(*eli),MEMF_PUBLIC | MEMF_CLEAR)) {
  406.  
  407.  
  408.                         addtags=&listtags[5];
  409.                         remtags=&listtags[7];
  410.                         edittags=&listtags[9];
  411.  
  412.                         list->GadgetTags=listtags;
  413.  
  414.                         for (x=0;x<5;++x) {
  415.                         listtags[x]=ListTags[x];
  416.                         edittags[x]=ListEditorTags[x];
  417.                         }
  418.  
  419.                         for (x=0;x<2;++x) {
  420.                         addtags[x]=ListAddTags[x];
  421.                         remtags[x]=ListDeleteTags[x];
  422.                         }
  423.  
  424.                         add->GadgetTags=addtags;
  425.                         rem->GadgetTags=remtags;
  426.                         edit->GadgetTags=edittags;
  427.  
  428.                         listtags[0].ti_Data=GetTagData(GTPK_List,
  429.                         0,gtc->GadgetTags);
  430.                         listtags[3].ti_Data=edit;
  431.  
  432.                         list->UserData=eli;
  433.                         add->UserData=eli;
  434.                         rem->UserData=eli;
  435.                         edit->UserData=eli;
  436.  
  437.                         AddInternalControl(req,gtc,list,0);
  438.                         AddInternalControl(req,gtc,add,0);
  439.                         AddInternalControl(req,gtc,rem,0);
  440.                         AddInternalControl(req,gtc,edit,0);
  441.  
  442.                         eli->Main=gtc;
  443.                         eli->Edit=edit;
  444.                         eli->Listc=list;
  445.                         eli->Add=add;
  446.                         eli->Delete=rem;
  447.                         eli->List=(struct List *)listtags[0].ti_Data;
  448.                         eli->Key=(struct Remember **)
  449.                          GetTagData(GTPK_Remember,0,gtc->GadgetTags);
  450.                         eli->Flags = GetTagData(GTPK_Alpha,0,
  451.                         gtc->GadgetTags) ? ALPHA : 0;
  452.                         eli->Size = GetTagData(GTPK_NodeSize,
  453.                         sizeof(struct Node),gtc->GadgetTags);
  454.  
  455.                         eli->SetLabels =
  456.                         GetTagData(GTPK_SetList,0,gtc->GadgetTags);
  457.  
  458.                         if (!(eli->List)) {
  459.                         addtags[0].ti_Data=
  460.                         remtags[0].ti_Data=
  461.                         edittags[0].ti_Data=1;
  462.                         }
  463.                         gtc->SetAttrs=SetEditListAttrs;
  464.                         gtc->ControlTags=(struct Gadget *)eli;
  465.                         gtc->Attribute=0xffffffff;
  466.                         return gad;
  467.                     }
  468.                     }
  469.                 }
  470.                 }
  471.             }
  472.             }
  473.         }
  474.         }
  475.     }
  476.     }
  477.     return 0;
  478. }
  479.  
  480. /* Rules for edittable list to follow:
  481.  
  482.     gadgetup(editor) :- ord=~0, not(blank(editor)), AddEntryToListBox(),
  483.              select(lastentry).
  484.     gadgetup(editor) :- ord=~0, blank(editor), select(firstentry).
  485.     gadgetup(editor) :- ord\=~0, not(blank(editor)), ChangeNodesName(ord).
  486.     gadgetup(editor) :- ord\=~0, blank(editor), gadgetup(delete).
  487.  
  488.     gadgetup(new) :- SetOrd(~0),ActivateGadget(editor).
  489.     gadgetup(delete) :- ord\=~0, RemoveEntryFromListBox(ord).
  490.  
  491. */
  492.  
  493. UBYTE SetUpdating(struct EditListInfo *eli)
  494. {
  495.     if (!(eli->Flags & UPDATING)) {
  496.     eli->Flags|=UPDATING;
  497.     return 1;
  498.     }
  499.     return 0;
  500. }
  501.  
  502. void ClearUpdating(UBYTE state,struct EditListInfo *eli)
  503. {
  504.     if (state) FLAGOFF(eli->Flags,UPDATING);
  505. }
  506.  
  507. static void DeleteGadgetUp(struct GTRequest *req,struct IntuiMessage *msg,
  508.             struct GTControl *gtc,struct MessageHandler *mh)
  509. {
  510.     struct EditListInfo *eli;
  511.     struct Node *n;
  512.     UBYTE state;
  513.  
  514.     eli=(struct EditListInfo *)gtc->UserData;
  515.     if (eli->List==0 || eli->List==(struct List *)0xffffffff) return;
  516.  
  517.     state=SetUpdating(eli);
  518.  
  519.     if (eli->Main->Attribute!=~0) {
  520.     if (n=OrdToNode(eli->List,eli->Listc->Attribute))
  521.         RemoveEntryFromListBox(req,eli->Main,eli->List,n);
  522.         msg->IAddress=(APTR)n;
  523.         SendMessageToControl(req,eli->Main,msg,"DeletedEntry");
  524.         msg->IAddress=(APTR)eli->List;
  525.         SendMessageToControl(req,eli->Main,msg,"ChangedList");
  526.         msg->Code=CountNodesInList(eli->List)-1;
  527.         msg->IAddress=(APTR)n;
  528.         SendMessageToControl(req,eli->Main,msg,"GadgetUp");
  529.     }
  530.     ClearUpdating(state,eli);
  531. }
  532.  
  533. static void NewGadgetUp(struct GTRequest *req,struct IntuiMessage *msg,
  534.             struct GTControl *gtc,struct MessageHandler *mh)
  535. {
  536.     struct EditListInfo *eli;
  537.     UBYTE state;
  538.  
  539.     eli=(struct EditListInfo *)gtc->UserData;
  540.     if (eli->List==0 || eli->List==(struct List *)0xffffffff || eli->Key==0) return;
  541.  
  542.     state=SetUpdating(eli);
  543.  
  544.     SetControlAttrs(req,eli->Main,GTLV_Selected,~0,TAG_DONE);
  545.     msg->Code=~0;
  546.     msg->IAddress=0;
  547.     SendMessageToControl(req,eli->Main,msg,"GadgetUp");
  548.     ActivateGadget(eli->Edit->Gadget,req->Window,0);
  549.  
  550.     ClearUpdating(state,eli);
  551. }
  552.  
  553. static void EditorDSelected(struct GTRequest *req,struct IntuiMessage *msg,
  554.             struct GTControl *gtc,struct MessageHandler *mh)
  555. {
  556.     struct EditListInfo *eli;
  557.     struct StringInfo *si;
  558.     UBYTE isblank;
  559.     UBYTE state;
  560.  
  561.     eli=(struct EditListInfo *)gtc->UserData;
  562.     if (eli->List==0 || eli->List==(struct List *)0xffffffff || eli->Key==0) return;
  563.  
  564.     state=SetUpdating(eli);
  565.  
  566.     si=(struct StringInfo *)eli->Edit->Gadget->SpecialInfo;
  567.     isblank=!(si->Buffer[0]);
  568.  
  569.  
  570.     if (eli->Main->Attribute==~0) {
  571.     if (!isblank) {
  572.         struct Node *n;
  573.  
  574.         n=AddEntryToListBox(eli->Key,req,eli->Main,eli->List,si->Buffer,
  575.          eli->Size,eli->Flags);
  576.  
  577.         msg->IAddress=n;
  578.         SendMessageToControl(req,eli->Main,msg,"AddedEntry");
  579.         msg->IAddress=eli->List;
  580.         SendMessageToControl(req,eli->Main,msg,"ChangedList");
  581.  
  582.         msg->Code=NodeToOrd(eli->List,n);
  583.         SetControlAttrs(req,eli->Listc,GTLV_Selected,
  584.         msg->Code,TAG_DONE);
  585.  
  586.         msg->IAddress=n;
  587.  
  588.         SendMessageToControl(req,eli->Main,msg,"GadgetUp");
  589.     } else {
  590.         SetControlAttrs(req,eli->Listc,GTLV_Selected,0,TAG_DONE);
  591.         msg->Code=0;
  592.         msg->IAddress=(APTR)OrdToNode(eli->List,0);
  593.         SendMessageToControl(req,eli->Main,msg,"GadgetUp");
  594.     }
  595.     } else {
  596.     if (!isblank) {
  597.         struct Node *n;
  598.  
  599.         if (n=OrdToNode(eli->List,eli->Main->Attribute))
  600.         ChangeNodesName(eli->Key,req,eli->Main,eli->List,n,si->Buffer);
  601.         msg->IAddress=n;
  602.         SendMessageToControl(req,eli->Main,msg,"ChangedEntry");
  603.         msg->IAddress=eli->List;
  604.         SendMessageToControl(req,eli->Main,msg,"ChangedList");
  605.     } else {
  606.         DeleteGadgetUp(req,msg,gtc,mh);
  607.     }
  608.     }
  609.     ClearUpdating(state,eli);
  610. }
  611.  
  612. static void ListGadgetUp(struct GTRequest *req,struct IntuiMessage *msg,
  613.             struct GTControl *gtc,struct MessageHandler *mh)
  614. {
  615.     struct EditListInfo *eli;
  616.  
  617.     eli=(struct EditListInfo *)gtc->UserData;
  618.  
  619.     eli->Main->Attribute=msg->Code;
  620.  
  621.     SendMessageToControl(req,eli->Main,msg,"GadgetUp");
  622. }
  623.  
  624.